package com.pbase;

import android.app.Application;
import android.view.Gravity;

import com.blankj.utilcode.util.Utils;
import com.hjq.toast.ToastUtils;
import com.hjq.toast.style.WhiteToastStyle;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStore;
import androidx.lifecycle.ViewModelStoreOwner;

//ViewModelStoreOwner 实现该接口的类，表示自身能够向外提供VM  ( 让vm与生命周期绑定的关键 )
public abstract class BaseApplication extends Application implements ViewModelStoreOwner {
    private ViewModelStore mAppViewModelStore;
    private ViewModelProvider mApplicationProvider;


    @Override
    public void onCreate() {
        super.onCreate();
        mAppViewModelStore = new ViewModelStore();
        Utils.init(this);
        ToastUtils.init(this);
        ToastUtils.setStyle(new WhiteToastStyle());//白底土司
        ToastUtils.setGravity(Gravity.BOTTOM, 0, 200);//设置土司在界面底部，默认居中

    }

    @NonNull
    @Override
    public ViewModelStore getViewModelStore() {
        return mAppViewModelStore;
    }
    protected <T extends ViewModel> T getApplicationScopeViewModel(@NonNull Class<T> modelClass) {
        if (mApplicationProvider == null) {
            mApplicationProvider = new ViewModelProvider(
                    this, ViewModelProvider.AndroidViewModelFactory.getInstance(this));
        }
        return mApplicationProvider.get(modelClass);
    }


}
